package ae.pcfc.cldos.online.web.controller.client.company.nocandletters;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.ModelAttribute;

import ae.pcfc.cldos.online.common.delegate.CountryDelegate;
import ae.pcfc.cldos.online.common.delegate.NocLetterDelegate;
import ae.pcfc.cldos.online.common.vo.LabelVO;
import ae.pcfc.cldos.online.common.vo.selectors.CountryVO;
import ae.pcfc.cldos.online.common.vo.selectors.SystemValuesVO;
import ae.pcfc.cldos.online.web.Constants;


/**
 * 
 * @author Sergey Manoylo
 *
 */
public class NocAndLettersCommonController extends NocAndLettersFnpCommonController {
	
	@Autowired
	private NocLetterDelegate nocLetterDelegate;
	
	@Autowired
	@Qualifier("countryDelegate")
	private CountryDelegate countryDelegate;
	
	@ModelAttribute(Constants.__COUNTRY_LIST_REQUEST_MODEL_KEY)
	public List<LabelVO> populateCountryList(Locale locale){
		List<LabelVO> labelList = new ArrayList<LabelVO>();
		LabelVO labelVO = new LabelVO();
		
		labelList.add(labelVO);
		
		List<CountryVO> countriesVO = countryDelegate.getContriesVOList();
		
		for (CountryVO countryVO : countriesVO) {
			labelVO = countryDelegate.getMapper().map(countryVO, LabelVO.class);
			labelList.add(labelVO);
		}
		return labelList;
	}
	
	private List<LabelVO> getLabelsBySystemValuesList(List<SystemValuesVO> systemValuesVOs) {
		
		List<LabelVO> labelList = new ArrayList<LabelVO>(systemValuesVOs.size());
		labelList.add(new LabelVO());
		
		for(SystemValuesVO systemValuesVO : systemValuesVOs) {
			LabelVO labelVO = systemValuesDelegate.getMapper().map(systemValuesVO, LabelVO.class);
			labelList.add(labelVO);
		}
		
		return labelList;
	}
	
	
	public NocLetterDelegate getNocLetterDelegate() {
		return nocLetterDelegate;
	}
	
	@ModelAttribute(Constants.__SIGNATURE_LIST_MODEL_KEY)
	public List<LabelVO> populateSignatureList(Locale locale){
		return getLabelsBySystemValuesList(getSystemValuesDelegate().getSignature());
	}
	
	@ModelAttribute(Constants.__NOC_LETTERS_LIST_MODEL_KEY)
	public List<LabelVO> populateNocLettersList(Locale locale) {
		return getLabelsBySystemValuesList(getSystemValuesDelegate().getCertificate());
	}

}
